package demo2reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author 12629
 * @Description：
 */
public class Test {


    public static void main(String[] args){
        //reflectNewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();
    }

    // 反射私有方法
    public static void reflectPrivateMethod() {
        try {
            Class<?> c1 = Class.forName("demo2reflect.Student");
            Method me = c1.getDeclaredMethod("function", String.class);

            me.setAccessible(true);

            Student student = (Student)c1.newInstance();
            me.invoke(student,"我是参数！");

        }catch (ClassNotFoundException e) {

        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    // 反射私有属性
    public static void reflectPrivateField() {
        try {
            Class<?> c1 = Class.forName("demo2reflect.Student");
            Field field = c1.getDeclaredField("name");
            field.setAccessible(true);

            Student student = (Student)c1.newInstance();

            field.set(student,"zhugeliang");

            System.out.println(student);
        }catch (ClassNotFoundException e) {

        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    // 反射私有的构造方法  屏蔽内容为获得公有的构造方法
    public static void reflectPrivateConstructor() {
        try {
            Class<?> c1 = Class.forName("demo2reflect.Student");
            Constructor<?> declaredConstructor =
                    c1.getDeclaredConstructor(String.class, int.class);
            declaredConstructor.setAccessible(true);

            Student student = (Student)declaredConstructor.newInstance("zhanagsan",10);

            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }



    // 创建对象
    public static void reflectNewInstance(){

        try {
            Class<?> c1 = Class.forName("demo2reflect.Student");
            Student student = (Student)c1.newInstance();

            System.out.println(student);

        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main1(String[] args) throws ClassNotFoundException {
        Class<?> c1 = Class.forName("demo2reflect.Student");

        Class<?> c2 = Student.class;

        Student student = new Student();
        Class<?> c3 = student.getClass();

        System.out.println(c1.equals(c2));
        System.out.println(c1.equals(c3));
        System.out.println(c3.equals(c2));
    }
}
